package cn.itsource.hrm.userdetails;

import cn.itsource.hrm.client.SysmanageServerClient;
import cn.itsource.hrm.constants.LoginConstants;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.domain.Permission;
import cn.itsource.hrm.mapper.LoginUserMapper;
import cn.itsource.hrm.mapper.PermissionMapper;
import cn.itsource.hrm.vo.UserContextInfo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

//@Service
public class HrmUserDetailsService implements UserDetailsService {
    @Autowired
    private LoginUserMapper loginUserMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private SysmanageServerClient sysmanageServerClient;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Wrapper<LoginUser> wrapper = new EntityWrapper<LoginUser>()
                .eq("username", username)
                .or()
                .eq("email", username)
                .or()
                .eq("phone", username);
        List<LoginUser> loginUsers = loginUserMapper.selectList(wrapper);
        if (loginUsers==null || loginUsers.size()<1)
            return null;

        LoginUser loginUser = loginUsers.get(0);
        //后台管理任意才去查询权限
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (loginUser.getType().intValue()== LoginConstants.LOGIN_TYPE_ADMIN){
            //1 从数据库中查询权限
            List<Permission> permissions = permissionMapper
                    .loadPermissionsByUserId(loginUser.getId());

            //2 转换为SPringsecurity所需要权限数据
            authorities = permissions.stream().map(permission -> {
                return new SimpleGrantedAuthority(permission.getSn());
            }).collect(Collectors.toList());

            UserContextInfo userContextInfo = sysmanageServerClient
                    .getEmpAndTenantByLoginId(loginUser.getId());
            userContextInfo.setLoginUserId(loginUser.getId());
            userContextInfo.setLoginUsername(loginUser.getUsername());
            System.out.println(userContextInfo);
            return new User(JSON.toJSONString(userContextInfo),loginUser.getPassword(),authorities);
        }else {
            UserContextInfo userContextInfo = new UserContextInfo();
            userContextInfo.setLoginUserId(loginUser.getId());
            userContextInfo.setLoginUsername(loginUser.getUsername());
            //userId username和上边一样，要远程发起请求去调用。 @TODO
            return new User(JSON.toJSONString(userContextInfo),loginUser.getPassword(),authorities);
        }

    }
}
